package com.zt.teacher.controller;

import com.zt.common.Do.PrivateLetterDO;
import com.zt.common.Do.PrivateLetterWithInfoDO;
import com.zt.common.pojo.PrivateLetter;
import com.zt.common.pojo.common.JsonResult;
import com.zt.teacher.dto.PrivateLetterDTO;
import com.zt.teacher.dto.PrivateLetterDetailDTO;
import com.zt.teacher.service.PrivateLetterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * description:
 * author:zt
 * date:2022-02-07
 */
@RestController
@CrossOrigin
@RequestMapping("privateLetter")
@Api("私信模块")
public class PrivateLetterController {

    @Autowired
    private PrivateLetterService privateLetterService;

    @GetMapping("get/{semester}")
    @ApiOperation("获取聊天记录")
    public JsonResult<ArrayList<PrivateLetterWithInfoDO>> getPrivateLetter(HttpServletRequest request,@PathVariable String semester){
        ArrayList<PrivateLetterWithInfoDO> list = privateLetterService.getRecord(request,semester);
        return JsonResult.success(list);
    }

    @PostMapping("insert")
    @ApiOperation("发送私信")
    public JsonResult<String> insert(@RequestBody PrivateLetterDTO privateLetterDTO, HttpServletRequest request){
        return JsonResult.success(privateLetterService.insert(privateLetterDTO,request));
    }

    @PostMapping("getDetail")
    @ApiOperation("获取聊天详情中的消息")
    public JsonResult<List<PrivateLetterDO>> getDetail(HttpServletRequest request, @RequestBody PrivateLetterDetailDTO privateLetterDetailDTO){
        List<PrivateLetterDO> detail = privateLetterService.getDetail(request, privateLetterDetailDTO);
        return JsonResult.success(detail);
    }

    @PostMapping("read")
    @ApiOperation("将消息修改为已读")
    public JsonResult readLetter(HttpServletRequest request,@RequestBody PrivateLetterDetailDTO privateLetterDetailDTO){
        privateLetterService.readLetter(request,privateLetterDetailDTO);
        return JsonResult.success();
    }

    @GetMapping("getUnreadCount/{semester}")
    @ApiOperation("获取未读消息数量")
    public JsonResult<Integer> getUnreadCount(HttpServletRequest request, @PathVariable String semester){
        int unreadCount = privateLetterService.getUnreadCount(request,semester);
        return JsonResult.success(unreadCount);
    }
}
